package com.ruoyi.coin.utils.ave;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.ruoyi.coin.enums.OklinkChainEnum;
import com.ruoyi.coin.model.ave.AveKlineModel;
import com.ruoyi.coin.model.ave.AvePhoneHotModel;
import com.ruoyi.coin.model.ave.AvePhoneHotTokenModel;
import com.ruoyi.coin.model.ave.AveTokenModel;
import com.ruoyi.common.config.CoinConfig;
import com.ruoyi.common.utils.blade.Func;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class AvueUtils {

    //获取手机端热门列表
    public static List<AvePhoneHotTokenModel> getPhoneIndexHotList() {
        List<AvePhoneHotTokenModel> hotTokenList = new ArrayList<>();
        try {
            String url = "https://api.dryespah.com/v1api/v2/tokens/hot";
            String body = sendHttp(url, CoinConfig.getAve_token());
            if(Func.isEmpty(body)){
                return hotTokenList;
            }

            AvePhoneHotModel hotModel = JSONUtil.toBean(body, AvePhoneHotModel.class);
            if (Func.isEmpty(hotModel)) {
                return hotTokenList;
            }
            String encodeData = hotModel.getEncode_data();
            String decodeStr = Base64.decodeStr(encodeData);
            String decode = URLUtil.decode(decodeStr);
            List<AvePhoneHotTokenModel> hotDataList = JSONUtil.toList(decode, AvePhoneHotTokenModel.class);

            if (Func.isEmpty(hotDataList)) {
                return hotTokenList;
            }
            for (AvePhoneHotTokenModel tokenModel : hotDataList) {
                String chain = tokenModel.getChain();
                OklinkChainEnum oklinkChainEnum = OklinkChainEnum.getByCode(chain);
                if(Func.isEmpty(oklinkChainEnum)){
                    continue;
                }
                Boolean openFalg = oklinkChainEnum.getOpenFalg();
                if(!openFalg){
                    continue;
                }

                Integer holders = tokenModel.getHolders();
                if (holders < 100) {//地址数少
                    continue;
                }
                Integer frontShowIndex = tokenModel.getFront_show_index();
                if (frontShowIndex > 10) {//只获取前十
                    continue;
                }
                hotTokenList.add(tokenModel);

            }

            return hotTokenList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hotTokenList;
    }

    //获取1小时K线
    public static Integer get1HourKlineCou(OklinkChainEnum oklinkChainEnum,String main_pair) {
        try {
            String url = "https://api.dryespah.com/v1api/v4/pairs/" + main_pair + "-" + oklinkChainEnum.getCode() + "/kline?interval=3600&category=u&count=800";
            String body = sendHttp(url,CoinConfig.getAve_token());
            if(Func.isEmpty(body)){
                return -1;
            }

            AveKlineModel aveKlineModel = JSONUtil.toBean(body, AveKlineModel.class);
            if (Func.isEmpty(aveKlineModel)) {
                return -1;
            }
            String encodeData = aveKlineModel.getEncode_data();
            String decodeStr = Base64.decodeStr(encodeData);
            String decode = URLUtil.decode(decodeStr);
            AveKlineModel.DataModel dataModel = JSONUtil.toBean(decode, AveKlineModel.DataModel.class);
            List<AveKlineModel.klineDataModel> klineDataModelList = dataModel.getKline_data();
            if (Func.isEmpty(klineDataModelList)) {
                return -1;
            }
            return klineDataModelList.size();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    //获取最新价格
    public static BigDecimal getNewPrice(OklinkChainEnum oklinkChainEnum,String main_pair) {
        try {
            String url = "https://api.dryespah.com/v1api/v4/pairs/" + main_pair + "-" + oklinkChainEnum.getCode() + "/kline?interval=300&category=u&count=800";
            String body = sendHttp(url,CoinConfig.getAve_token());
            if(Func.isEmpty(body)){
                return BigDecimal.ZERO;
            }

            AveKlineModel aveKlineModel = JSONUtil.toBean(body, AveKlineModel.class);
            if (Func.isEmpty(aveKlineModel)) {
                return BigDecimal.ZERO;
            }
            String encodeData = aveKlineModel.getEncode_data();
            String decodeStr = Base64.decodeStr(encodeData);
            String decode = URLUtil.decode(decodeStr);
            AveKlineModel.DataModel dataModel = JSONUtil.toBean(decode, AveKlineModel.DataModel.class);
            List<AveKlineModel.klineDataModel> klineDataModelList = dataModel.getKline_data();
            if (Func.isEmpty(klineDataModelList)) {
                return BigDecimal.ZERO;
            }
            AveKlineModel.klineDataModel lastModel = klineDataModelList.get(klineDataModelList.size() - 1);
            BigDecimal close = lastModel.getClose();
            return close;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BigDecimal.ZERO;

    }

    //获取币种详情
    public static AveTokenModel.DataModel getAveTokenInfoModel(OklinkChainEnum oklinkChainEnum, String contractToken) {
        String url = "https://api.dryespah.com/v1api/v3/tokens/" + contractToken + "-" + oklinkChainEnum.getCode();
        String body = sendHttp(url,CoinConfig.getAve_token());
        if(Func.isEmpty(body)){
            return null;
        }

        AveTokenModel aveTokenModel = JSONUtil.toBean(body, AveTokenModel.class);
        if (Func.isEmpty(aveTokenModel)) {
            return null;
        }
        String msg = aveTokenModel.getMsg();
        if (!Func.equals(msg, "SUCCESS")) {
            return null;
        }
        String data = aveTokenModel.getData();
        if (Func.isEmpty(data)) {
            return null;
        }
        AveTokenModel.DataModel dataModel = JSONUtil.toBean(data, AveTokenModel.DataModel.class);
        return dataModel;
    }

    public static AveTokenModel.Tokenodel getAveTokenInfo(OklinkChainEnum oklinkChainEnum, String contractToken) {
        AveTokenModel.DataModel dataModel = getAveTokenInfoModel(oklinkChainEnum, contractToken);
        AveTokenModel.Tokenodel token = dataModel.getToken();
        return token;
    }


    private static String sendHttp(String url,String authToken){
        try{
            String body = HttpRequest.get(url)
                    .header("x-auth", authToken)
                    .execute().body();
            return body;
        }catch (Exception e){

        }
        return null;
    }

}
